package com.jams.goodox.activity.mine.settings;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lvman.configs.instrumentation.InstrumentedDraweeView;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.entity.SuggestionDetail;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.MineService;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.ToastUtil;
import com.jams.library.fresco.FrescoUtil;
import com.jams.library.image.ImagePagerActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import retrofit2.Call;

/**
 * Created by Jin on 2016/6/4.
 * Description 我的建议列表详情
 */
public class IndexSuggestListDetail extends BaseActivity {

	@Bind(R.id.time)
	TextView time;
	@Bind(R.id.suggestion)
	TextView suggestion;
	@Bind(R.id.reply_name)
	TextView replyName;
	@Bind(R.id.reply_content)
	TextView replyContent;
	@Bind(R.id.reply_time)
	TextView replyTime;
	@Bind(R.id.reply_layout)
	LinearLayout replyLayout;
	@Bind(R.id.img_layout)
	LinearLayout imgLayout;
	@Bind(R.id.temp)
    InstrumentedDraweeView temp;
	@Bind(R.id.img_layout2)
	LinearLayout imgLayout2;
	@Bind(R.id.temp2)
    InstrumentedDraweeView temp2;
	@Bind(R.id.line)
	ImageView line;

	private Activity activity;

	private String id;

	@Override
	protected int getLayoutId() {
		return R.layout.mine_suggest_list_detail;
	}

	@Override
	protected void init() {
		activity = this;
		StyleUtil.init(this, "建议详情");

		id = getIntent().getStringExtra("id");

		initDatas();
	}

	private void initDatas() {
		enqueue(createCallService(MineService.class).getSuggestionDetail(id), new RetrofitCallbackListener<SuggestionDetail>() {
			@Override
			public void onSuccess(Call<SuggestionDetail> call, SuggestionDetail result) {
				SuggestionDetail.DataBean bean = result.getData();

				if (null != bean) {
					time.setText(bean.getIntime());
					suggestion.setText(bean.getSuggestion());
					initMineImgs(bean.getAnnexs());

					line.setVisibility(bean.getReplyStatus() == 0 ? View.GONE : View.VISIBLE);
					replyLayout.setVisibility(bean.getReplyStatus() == 0 ? View.GONE : View.VISIBLE);
					if (bean.getReplyUserName() != null) {
						replyName.setText(bean.getReplyUserName() + " 回复 :");
						replyContent.setText(bean.getReply());
						replyTime.setText(bean.getReplyTime());
						initReplyImgs(bean.getReplyImg());
					}
				}
			}

			@Override
			public void onError(String msg) {
				ToastUtil.show(msg);
			}
		});
	}

	private void initMineImgs(List<SuggestionDetail.DataBean.AnnexsBean> list) {
		if (null == list)
			return;
		imgLayout.setVisibility(list.size() == 0 ? View.GONE : View.VISIBLE);
		int mar = getResources().getDimensionPixelSize(R.dimen.margin_size_small);

		// 动态添加图片  4个的时候，总的宽度要顶到边上
		if (list.size() != 0) {

			List<String> listTemp = new ArrayList<>();
			for (SuggestionDetail.DataBean.AnnexsBean ab : list) {
				listTemp.add(ab.getName());
			}

			for (SuggestionDetail.DataBean.AnnexsBean ab : list) {
				InstrumentedDraweeView imgView = (InstrumentedDraweeView) getLayoutInflater().inflate(R.layout.mine_suggest_list_detail_img, imgLayout, false);
				FrescoUtil.loadImgWithResize(imgView, ab.getName(), 200, 200);

				if (imgLayout.getChildCount() > 1) {
					LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) temp.getLayoutParams();
					params.leftMargin = mar;
					if (list.size() == 4) {
						params.width = (imgLayout.getWidth() - mar * 3) / 4;
						params.height = params.width;
					}
					imgView.setLayoutParams(params);
				}

				imgView.setOnClickListener(new MyImgsClickListener(listTemp, imgLayout.getChildCount() - 1));
				imgLayout.addView(imgView);
			}

			if (list.size() == 4) {
				LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imgLayout.getChildAt(1).getLayoutParams();
				params.width = (imgLayout.getWidth() - mar * 3) / 4;
				params.height = params.width;
				params.leftMargin = 0;
				imgLayout.getChildAt(1).setLayoutParams(params);
			}

			imgLayout.invalidate();
		}
	}

	private void initReplyImgs(List<SuggestionDetail.DataBean.ReplyImgBean> list) {

		if (null == list)
			return;

		imgLayout2.setVisibility(list.size() == 0 ? View.GONE : View.VISIBLE);
		int mar = getResources().getDimensionPixelSize(R.dimen.margin_size_small);

		// 动态添加图片  4个的时候，总的宽度要顶到边上
		if (list.size() != 0) {

			List<String> listTemp = new ArrayList<>();
			for (SuggestionDetail.DataBean.ReplyImgBean ab : list) {
				listTemp.add(ab.getName());
			}

			for (SuggestionDetail.DataBean.ReplyImgBean ab : list) {
				InstrumentedDraweeView imgView = (InstrumentedDraweeView) getLayoutInflater().inflate(R.layout.mine_suggest_list_detail_img, imgLayout2, false);
				FrescoUtil.loadImgWithResize(imgView, ab.getName(), 200, 200);

				if (imgLayout2.getChildCount() > 1) {
					LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) temp2.getLayoutParams();
					params.leftMargin = mar;
					if (list.size() == 4) {
						params.width = (imgLayout2.getWidth() - mar * 3) / 4;
						params.height = params.width;
					}
					imgView.setLayoutParams(params);
				}

				imgView.setOnClickListener(new MyImgsClickListener(listTemp, imgLayout2.getChildCount() - 1));
				imgLayout2.addView(imgView);
			}

			if (list.size() == 4) {
				LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imgLayout2.getChildAt(1).getLayoutParams();
				params.width = (imgLayout2.getWidth() - mar * 3) / 4;
				params.height = params.width;
				params.leftMargin = 0;
				imgLayout2.getChildAt(1).setLayoutParams(params);
			}

			imgLayout2.invalidate();
		}
	}

	class MyImgsClickListener implements View.OnClickListener {

		private List<String> list;
		private int index;

		public MyImgsClickListener(List<String> list, int index) {
			this.list = list;
			this.index = index;
		}

		@Override
		public void onClick(View v) {
			Bundle b = new Bundle();
			b.putSerializable(ImagePagerActivity.EXTRA_IMAGE_URLS, (Serializable) list);
			b.putSerializable(ImagePagerActivity.EXTRA_IMAGE_INDEX, index);
			qStartActivity(ImagePagerActivity.class, b);
		}
	}
}
